package net.helix.engine.library;

import java.io.Serializable;
import java.util.ArrayList;

import net.helix.engine.Engine;
import net.helix.engine.library.items.LibraryItem;

/**
 * The library class which holds all of the data for the project in a single list and contains a tree like structure that
 * contains folders and an id link to their associated items in the list.
 * @author Runouw
 *
 */
public class Library implements Serializable{
	private static final long serialVersionUID = 1L;

	/**
	 * The title of the library.
	 */
	private String name;
	
	private ArrayList<LibraryItem> items;
	private LibraryTreeNode tree;
	
	private Engine engine;
	public Library(Engine engine, String name){
		this.engine = engine;
		this.name = name;
		
		items = new ArrayList<LibraryItem>();
		tree = new LibraryTreeNode(this, LibraryTreeNode.FOLDER, "root");
	}
	/**
	 * Adds a new item. Note doesn't update graphics in the ui's library pane.
	 * @param item
	 */
	public void addNewItem(LibraryItem item, LibraryTreeNode parent){
		int id = items.size();
		items.add(item);
		item.id = id;
		
		LibraryTreeNode node = new LibraryTreeNode(this, id, item.name);
		
		// TODO: check if parent is an incorrect value
		if(true){
			parent.addNode(node);
			
		}
	}
	/**
	 * Adds a new folder. Note, doesn't update the graphics in the ui's library pane.
	 */
	public void addNewFolder(LibraryTreeNode parent){
		
		LibraryTreeNode node = new LibraryTreeNode(this, LibraryTreeNode.FOLDER, "new folder");
		// TODO: get selected item in library to determine where it imports to
		int index = -1;
		
		// TODO: check if parent is an incorrect value
		if(true){
			index = parent.children.size();
			parent.addNode(node);
		}
		
	}
	/**
	 * Get the top node of the tree structure. For use in displaying the library in the ui for example.
	 * @return
	 */
	public LibraryTreeNode getTreeRoot(){
		return tree;
	}
	/**
	 * Get the item associated with this id.
	 * @param id
	 * @return
	 */
	public LibraryItem getItem(int id){
		return items.get(id);
	}
	/**
	 * Get the number of items in this library.
	 * @return
	 */
	public int getNumberOfItems(){
		return items.size();
	}
	public int addItem(LibraryItem item){
		int id = items.size();
		items.add(item);
		item.id = id;
		return id;
	}
}
